function post(_url, data, cb, error_cb, com_cb) {
  var app = getApp();
  if (data == null) {
    data = {}
  }
  var token = wx.getStorageSync("token")
  if (token != null) {
    data.token = token
  }
 
  var url = app.config.apiurl + _url
  wx.request({
    url: url,
    data: data,
    method: 'POST',
    success: function(res) {
      var code = res.statusCode
      if (code != 200) {
        app.toast("接口异常:" + code + "|" + _url)
        return;
      }
      data = res.data
      if (typeof data != 'object') {
        app.toast("结果集不是JSON" + _url)
        return;
      }

      var error_code = data.error_code
      if (error_code == 0) {
        cb(data.result)
      } else if (error_code == 300) {
        if ("api/site/init" == _url) {
          error_cb()
        } else {
          getApp().site = null
          // wx.reLaunch({
          //   url: '/pages/index/index/index',
          // })
        }
      } else if (error_code == 301) {
        wx.navigateTo({
          url: '/pages/user/edit/index',
        })

      } else {
        if (error_cb == null) {
          app.toast(data.message)
        } else {
          error_cb(data.message, error_code)
        }
      }
    },
    fail: function(res) {
      app.toast("接口请求失败" + _url)
    },
    complete: function(res) {
      if (com_cb != null) {
        com_cb()
      }

    },
  })
}

function upload(dir, filePath, cb, _type) {
  var app = getApp();
  var data = {}
  var token = wx.getStorageSync("token")
  if (token != null) {
    data.token = token
  }
  data.dir = dir

  var url = app.config.apiurl + "api/index/upload"
  wx.uploadFile({
    url: url,
    filePath: filePath,
    name: 'file',
    formData: data,
    success: function(res) {
      var data = JSON.parse(res.data)
      if (data.error_code == 0) {
        cb(data.result)
      }

    }
  })
}

function check(that) {
  if (getApp().user_time != null) {
    let ctime = (new Date()).valueOf()
    if (ctime - getApp().user_time < 60000) {
      init(that)
      return;
    }
  }

  post("api/site/init", null, function(data) {
    getApp().user_time = (new Date()).valueOf();
    getApp().site = data
    init(that)

  }, function() {
    getApp().login(function(data) {
      check(that)
    })
  })
}



function init(that) {

  that.setData({
    resurl: getApp().config.resurl
  })
  that.setData({
    site: getApp().site
  })
  that.init()
}

function cache(name, val) {
  name = name + getApp().config.appid;
  if (val == null) {
    var v = wx.getStorageSync(name);
    v = v == "" ? null : v;
    return v;
  }
  if (val == "del") {
    wx.removeStorageSync(name)
    return;
  }
  wx.setStorageSync(name, val)

}
function pay(api,data,success_cb,error_cb){
  post(api, data, function (data) {
    wx.requestPayment({
      'timeStamp': data.timestamp + '',
      'nonceStr': data.nonceStr,
      'package': data.package,
      'signType': 'MD5',
      'paySign': data.paySign,
      'success': function (ref) {
        success_cb()
      },
      'fail': function (ref) {
        error_cb()
      }
    })
  })
}

exports.post = post
exports.upload = upload
exports.check = check
exports.cache = cache
exports.pay = pay